package cn.cfoodc.sharding.controller;

import cn.cfoodc.sharding.domain.TOrderItem;
import cn.cfoodc.sharding.service.OrderService;
import cn.cfoodc.sharding.service.TOrderItemService;
import org.apache.shardingsphere.sharding.algorithm.keygen.SnowflakeKeyGenerateAlgorithm;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

/**
 * 测试单表基本使用
 */
@RestController
public class DemoOrderItemController {


    @Autowired
    private TOrderItemService tOrderItemService;


    @Autowired
    private SnowflakeKeyGenerateAlgorithm NEXT_ID ;

    @GetMapping("/getOrderItemAndArgs_1")
    public TOrderItem getOne(@RequestParam("userId") Long userId , @RequestParam("orderItemId") Long orderItemId){
        return tOrderItemService.selectByPrimaryKey(orderItemId,userId);
    }


    /**
     * 增加11该笔订单详情
     * @return
     */
    @PostMapping("/insertOrderItem")
    @Transactional(rollbackFor = { Exception.class })
    @ShardingTransactionType(TransactionType.XA)// 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
    public int insert() {
        Long a = (Long) NEXT_ID.generateKey();
        for (int i = 0 ; i < 11 ; i++) {
            Long id = (Long) NEXT_ID.generateKey();
            TOrderItem t = new TOrderItem();
            t.setCreateDatetime( new Timestamp(System.currentTimeMillis()));
            t.setName(id + "");
            t.setOrderId(a);
            t.setOrderItemId(id);
            t.setUserId(a);
            tOrderItemService.insert(t);
        }
        //验证事务回滚是否触发，可以触发回滚。
        //int aa = 1/0;
        return 1;
    }


    //按照user_id和order_id分页查询订单详情
    @GetMapping("/getOrderItemPageAndArgs_1")
    public List<TOrderItem> getPage(@RequestParam("userId") Long userId , @RequestParam("orderId") Long orderId, @RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize){
        return tOrderItemService.selectPage(  userId , orderId , pageIndex < 1 ? 0 : (pageIndex-1)*pageSize , pageSize );
    }

    //全库全表查询，结果集归并在分页。
    @GetMapping("/getAllOrderItemPage")
    public List<TOrderItem> getPage(@RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize){
        return tOrderItemService.selectAllPage(pageIndex < 1 ? 0 : (pageIndex-1)*pageSize , pageSize );
    }


    @PostMapping("/updateOrderItemAndArgs")
    @Transactional(rollbackFor = { Exception.class })
    @ShardingTransactionType(TransactionType.XA)// 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
    public int updateOrderItemOne(@ModelAttribute TOrderItem tOrderItem){
        return tOrderItemService.updateByPrimaryKey(tOrderItem);
    }

    @PostMapping("/deleteOrderItemAndArgs")
    @Transactional(rollbackFor = { Exception.class })
    @ShardingTransactionType(TransactionType.XA)// 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
    public int deleteOrderItemOne(@RequestParam("orderItemId") Long orderItemId ,@RequestParam("userId") Long userId){
        return tOrderItemService.deleteByPrimaryKey(orderItemId , userId);
    }

}
